package Negocio;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletContext;

/**
 *
 * @author grodriguezpeinado & asabido
 */
public class MessageLogger {
    
    public static final String LOG = "yyyy-MM-dd HH:MM:ss";
    private String path;
    
//    static final Logger logger = Logger.getLogger("MyLog");
//    FileHandler fh;

    public MessageLogger() {
    }

    public MessageLogger(ServletContext servletContext) {
        this.path = servletContext.getRealPath("/");
    }
    
    
//    public void log(String msg){
//        try {
//            fh = new FileHandler("admin.log", true);
//            logger.addHandler(fh);
//            logger.setLevel(Level.ALL);
//            SimpleFormatter formatter = new SimpleFormatter();
//            fh.setFormatter(formatter);
//            logger.log(Level.INFO,msg);
//            
//        } catch (IOException ex) {
//            Logger.getLogger(MessageLogger.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (SecurityException ex) {
//            Logger.getLogger(MessageLogger.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    
    public void log(String msg) throws IOException{        
        String webPath = path.replace("build/","");
        BufferedWriter writer = new BufferedWriter(new FileWriter(webPath+"log",true));
        writer.write("\n["+fecha()+"]: "+msg);
        writer.close();
    }
    
    public static String fecha() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(LOG);
        return sdf.format(cal.getTime());
    }
    
}
